import { CreateOrUpdateUserComponent } from './create-or-update-user/create-or-update-user.component';
import { UserServiceProxy } from './../../../shared/service-proxies/service-proxies';
import { Component, OnInit, Injector } from '@angular/core';
import { PagedListingComponentBase, PagedRequestDto } from '@shared/component-base/paged-listing-component-base';
import { UserDto } from '@shared/service-proxies/service-proxies';
import { STPage, STColumn, STChange } from '@delon/abc';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styles: []
})
export class UserComponent extends PagedListingComponentBase<UserDto> {

  filter: string;
  protected fetchData(request: PagedRequestDto, pageNumber: number, finishedCallback: Function): void {

    this.userServiceProxy
      .getAll("", "", undefined, undefined, undefined, request.skipCount, request.maxResultCount)
      .finally(() => {
        finishedCallback();
      })
      .subscribe((result) => {
        this.dataList = result.items;
        this.totalItems = result.totalCount;
      });
  }
  protected delete(entity: UserDto): void {
    this.userServiceProxy.delete(entity.id).finally(() => { }).subscribe(res => {
      this.refresh();
    });
  }

  hasCreate: boolean;
  hasDelete: boolean;
  hasUpdate: boolean;
  constructor(
    public userServiceProxy: UserServiceProxy,
    injector: Injector) {
    super(injector);
    this.hasCreate = true;
    this.hasDelete = true;
    this.hasUpdate = true;
  }

  page: STPage = {
    front: false,
    total: true,
    showSize: true
  };
  show($event: STChange) {
    console.log($event);
    if ($event.type == "click") return;
    this.pageNumber = $event.pi;
    this.pageSize = $event.ps;
    this.refresh();
  }

  columns: STColumn[] = [
    { title: '账号', index: 'userName' },
    { title: '姓名', index: 'name' },
    { title: '邮箱', index: 'emailAddress' },
    { title: '状态', index: 'isActive', type: 'yn' },
    {
      title: '操作区',
      buttons: [
        {
          text: '删除',
          type: 'del',
          iif: () => this.hasDelete,
          click: (record: any) => {
            this.delete(record);
          }
        },
        {
          text: '编辑',
          iif: () => this.hasUpdate,
          click: (record: any) => {
            this.edit(record)
          }
        }
      ],
    },
  ];

  edit(item: UserDto): void {
    this.modalHelper.open(CreateOrUpdateUserComponent, { id: item.id })
      .subscribe(x => {
        this.refresh();
      });
  }

  create() {
    this.modalHelper.open(CreateOrUpdateUserComponent, {})
      .subscribe(x => {
        this.refresh();
      });
  };
}
